package com.bjsxt.service.imp;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bjsxt.commons.ToExcel;
import com.bjsxt.mapper.ApplicationlogMapper;
import com.bjsxt.mapper.DrugMapper;
import com.bjsxt.mapper.ProviderMapper;
import com.bjsxt.pojo.Applicationlog;
import com.bjsxt.pojo.ApplicationlogExample;
import com.bjsxt.pojo.ApplicationlogExample.Criteria;
import com.bjsxt.pojo.Drug;
import com.bjsxt.pojo.DrugExample;
import com.bjsxt.pojo.Provider;
import com.bjsxt.pojo.ProviderExample;
import com.bjsxt.service.WarehouseService;
/**
 * 查询库存信息
 * @author qxx
 *
 */
@Service
public class WarehouseServiceImpl implements WarehouseService {
	@Autowired
	private DrugMapper dm;
	@Autowired
	private ApplicationlogMapper am;
	@Autowired
	private ProviderMapper pm;
	
	@Override
	public List<Drug> findWarehouseInfo() {
		DrugExample de = new DrugExample();
		//获取药品信息集合
		List<Drug> list = dm.selectByExample(de);
		//将药品信息中的生产商id转换为名字
			//遍历集合
			for(Drug d:list){
				//生产商id
				Integer id = d.getProviderid();
				System.out.println(id);
				ProviderExample pe = new ProviderExample();
				//获取当前生产商信息
				Provider pro = pm.selectByPrimaryKey(id);
				//将生产商名字放入drug中
				System.out.println(pro.getProvidername());
				d.setProname(pro.getProvidername());
			}
		return list;
	}
	/**
	 * 添加申请记录
	 * @param 封装的信息 
	 */
	@Override
	public void addApplication(Applicationlog al) {
		am.insertSelective(al);
	}
	/**
	 * 状态status:
	 * 0:未审查
	 * 1:同意
	 * 2:不同意
	 */
	@Override
	public List<Applicationlog> findApplication() {
		//请求状态(请求所有未批准的请求)
		int status=0;
		ApplicationlogExample ae = new ApplicationlogExample();
		Criteria c = ae.createCriteria();
		c.andIsagreeEqualTo(status);
		List<Applicationlog> list = am.selectByExample(ae);
		return list;
	}
	/**
	 * 返回请求日志的集合
	 */
	@Override
	public List<Applicationlog> findAllApplication() {
		ApplicationlogExample ae = new ApplicationlogExample();
		return am.selectByExample(ae);
	}
	@Override
	public List<Provider> findProInfo() {
		ProviderExample pe = new ProviderExample();
		List<Provider> list = pm.selectByExample(pe);
		return list;
	}
	@Override
	public void outPutInfo() {
		List<Drug>list=findWarehouseInfo();
		int rowNum=list.size();
		String [][]arr=new String[rowNum][4];
		for(int i =0;i<rowNum;i++){
			arr[i][0]=(String.valueOf(list.get(i).getDrugid()));
			arr[i][1]=list.get(i).getDrugname();
			arr[i][2]=list.get(i).getProname();
			arr[i][3]=String.valueOf(list.get(i).getDrugnum());
			System.out.println(arr[i][0]+"\t"+arr[i][1]+"\t"+arr[i][2]+"\t"+arr[i][3]+"\t");
		}
		ToExcel te=new ToExcel();
		try {
			te.WriteExcel(arr, "D:/Info.xlsx");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	@Override
	public void updateAgree(Integer logid,Integer applicationnum,String drugname) {
		ApplicationlogExample ae=new ApplicationlogExample();
		Criteria c=ae.createCriteria();
		c.andLogidEqualTo(logid);
		Applicationlog al = new Applicationlog();
		al.setIsagree(1);
		am.updateByExampleSelective(al, ae);
		
		DrugExample de = new DrugExample();
		com.bjsxt.pojo.DrugExample.Criteria criteria = de.createCriteria();
		criteria.andDrugnameEqualTo(drugname);
		Integer count = dm.selectByExample(de).get(0).getDrugnum()+applicationnum;
		
		Drug drug = new Drug();
		drug.setDrugnum(count);
		dm.updateByExampleSelective(drug, de);
	}

	@Override
	public void dropApplication(Integer logid) {
		am.deleteByPrimaryKey(logid);
	}
	@Override
	public List<Applicationlog> findAgree() {
		ApplicationlogExample ae=new ApplicationlogExample();
		Criteria c=ae.createCriteria();
		c.andIsagreeEqualTo(1);
		List<Applicationlog> list=am.selectByExample(ae);
		return list;
	}
}
